package com.pucheng.bracelet.ui.fragment.bracelet

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.observe
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.app.weight.recyclerview.DividerGridItemDecoration
import com.pucheng.bracelet.app.weight.recyclerview.GridDividerItemDecoration
import com.pucheng.bracelet.app.weight.recyclerview.RecyclerSpace
import com.pucheng.bracelet.data.bean.WatchInfoBean
import com.pucheng.bracelet.databinding.FragmentBraceletBinding
import com.pucheng.bracelet.ui.activity.SettingActivity
import com.pucheng.bracelet.ui.activity.clock.ClockActivity
import com.pucheng.bracelet.ui.activity.message.MsgPushActivity
import com.pucheng.bracelet.ui.activity.upgrade.UpgradeActivity
import com.pucheng.bracelet.ui.activity.watch.WatchActivity
import com.pucheng.bracelet.ui.activity.watch.WatchScanActivity
import com.pucheng.bracelet.ui.adapter.BraceletMenuAdapter
import com.pucheng.bracelet.viewmodel.bracelet.BraceletViewModel
import com.zyyoona7.popup.EasyPopup
import kotlinx.android.synthetic.main.fragment_bracelet.*
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment
import me.hgj.jetpackmvvm.demo.app.base.BaseFragment
import me.hgj.jetpackmvvm.demo.app.ext.setOnViewClick
import me.hgj.jetpackmvvm.demo.app.ext.showPopup

class BraceletFragment : BaseFragment<BraceletViewModel, FragmentBraceletBinding>() {
    private lateinit var mAdapter: BraceletMenuAdapter

    override fun layoutId(): Int = R.layout.fragment_bracelet
    override fun lazyLoadData() {

    }

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.appVM = appViewModel
        mDatabind.apply {
            mViewModel.changeWatch(
                !TextUtils.isEmpty(
                    if (appViewModel.watchInfo.value != null) {
                        appViewModel.watchInfo.value!!.mac
                    } else ""
                )
            )
            rvMenu.apply {
                mAdapter = BraceletMenuAdapter()
                layoutManager = GridLayoutManager(requireContext(), 2)

                addItemDecoration(
                    DividerGridItemDecoration(requireContext())
                )
                adapter = mAdapter
                mAdapter.onItemClickListener = object : BraceletMenuAdapter.OnItemClickListener{
                    override fun onItemClick(item: Any) {
                        var intent: Intent? = null
                        when(item){
                            "拍照控制" -> {
                                mViewModel.startCamera()
                            }
                            else ->{
                                when (item) {
                                    "表盘选择" -> {
                                        intent = Intent(context, WatchActivity::class.java)
                                    }
                                    "消息推送" -> {
                                        intent = Intent(context, MsgPushActivity::class.java)
                                    }
                                    "日常闹钟" -> {
                                        intent = Intent(context, ClockActivity::class.java)
                                    }
                                    "其他设置" -> {
                                        intent = Intent(context, SettingActivity::class.java)
                                    }
                                    "固件升级" -> {
                                        intent = Intent(context, UpgradeActivity::class.java)
                                    }
                                }
                                intent?.apply {
                                    context.startActivity(this)
                                }
                            }
                        }

                    }

                }
            }
            tvBindDevice.click {
                mViewModel.changeWatch(true)
                startActivity(Intent(mActivity, WatchScanActivity::class.java))
            }
            tvUnbindDevice.click {
                requireContext().showPopup(
                    clBracelet,
                    R.layout.confirm_popup
                )
                    .setOnViewClick(R.id.tv_confirm) { _: View, easyPopup: EasyPopup ->
                        mViewModel.changeWatch()
                        appViewModel.watchInfo.value = WatchInfoBean("", "")
                        CacheUtil.setWatchInfo(WatchInfoBean("", ""))
                        easyPopup.dismiss()
                    }
                    .setOnViewClick(R.id.tv_cancel) { _: View, easyPopup: EasyPopup ->
                        easyPopup.dismiss()
                    }
            }
        }

    }

    override fun createObserver() {
        appViewModel.watchInfo.observe(this) {

            if (TextUtils.isEmpty(it.mac)) {
                cl_unbind.visibility = View.INVISIBLE
                cl_bind.visibility = View.VISIBLE
                mViewModel.changeWatch(false)
            } else {
                cl_unbind.visibility = View.VISIBLE
                cl_bind.visibility = View.INVISIBLE
                tv_device_name.text = it.name
                tv_bt_id.text = it.mac
                mViewModel.changeWatch(true)
            }
        }
        mViewModel.apply {
            menuDatas.observe(this@BraceletFragment) {
                mAdapter.submitList(it)
            }
            isStartCamera.observe(this@BraceletFragment) {
                if(it){
                    val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) // 启动系统相机
                    (context as Activity).startActivityForResult(intent, 101)
                }else{

                }
            }
        }
    }

    override fun dismissLoading() {
    }

    override fun showLoading(message: String) {
        ToastUtils.showShort(message)
    }

}